package Persistence;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;


/**
 *
 * @author DRAIKO
 * 
 * Class to save the data in a PDF document
 */
public class PDFDocument {

    private static Font boldBig = new Font(Font.FontFamily.TIMES_ROMAN, 18,
            Font.BOLD);
    private static Font bold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
            Font.BOLD);
    

    /*
     * Create the document with the data organized in a table
     */
    public static void createDocument(String documentName, String data) {
        Document document = new Document();
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream(documentName + ".pdf"));

            document.open();

            Paragraph paragraph = new Paragraph("Forecast", boldBig);
            paragraph.setAlignment(Element.ALIGN_CENTER);
            paragraph.add(new Paragraph(" "));

            PdfPTable table = new PdfPTable(2); // number of columns.
            table.setWidthPercentage(60);
            
            //Separate the lines of the data
            String[] dataRow = data.split("[\n\r]");
            String[] aux;
            
            PdfPCell cell = new PdfPCell(new Paragraph("Date", bold));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            
            cell = new PdfPCell(new Paragraph("Weather °C", bold));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            
            for (int i = 1; i < dataRow.length - 1; i++) {
                aux = dataRow[i].split(" :      ");
                for (int j = 0; j < aux.length; j++) {
                    cell = new PdfPCell(new Paragraph(aux[j]));
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cell);
                }
            }
            

            document.add(paragraph);
            document.add(table);
            document.add(new Paragraph("\n" + dataRow[dataRow.length-1]));
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error");
        }
    }

    /*
     * Create a document with just the String
     */
    public static void createDocument(String documentName, String[] data) {
        Document document = new Document();

        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream(documentName + ".pdf"));

            document.open();
            Paragraph paragraph = new Paragraph("Forecast", boldBig);
            paragraph.setAlignment(Element.ALIGN_CENTER);
            paragraph.add(new Paragraph(" "));

            document.add(paragraph);
            document.add(new Chunk(data[0]));
            document.close();

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}